#pragma once
namespace JRG {
	template <class T>
	class Greater {
	public:
		bool operator()(const T& x, const T& y) {
			return x < y;
		}
	};
	template <class T>
	class Less {
	public:
		bool operator()(const T& x, const T& y) {
			return x > y;
		}
	};
	template<class T,class Container = vector<T>,class Compare = Less<T>>
	class Priority_Queue {
	public:
		void adjustdown(size_t parent) {
			Compare com;
			size_t child = parent * 2 + 1;
			while (child < size()) {
				if (child + 1 < size() && com(_con[child+1] , _con[child])) {
					child++;
				}
				if (com(_con[child] , _con[parent])) {
					std::swap(_con[child], _con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else {
					break;
				}
			}
		}
		void pop() {
			std::swap(_con[0], _con[size() - 1]);
			_con.pop_back();
			adjustdown(0);
		}
		void adjustup(size_t child) {
			Compare com;
			size_t parent = (child - 1) / 2;
			while (child > 0) {
				if (com(_con[child] , _con[parent])) {
					std::swap(_con[child], _con[parent]);
					child = parent;
					parent= (child - 1) / 2;
				}
				else {
					break;
				}
			}
		}
		void push(const T& val) {
			_con.push_back(val);
			adjustup(size() - 1);
		}

		void swap(Priority_Queue& q) {
			std::swap(*this, q);
		}
		size_t size() {
			return _con.size();
		}

		const T& top() {
			return _con[0];
		}
		bool empty() {
			return _con.empty();
		}
	private:
		Container _con;
	};
	void test1() {
		Priority_Queue<int,std::deque<int>> pq;
		pq.push(11);
		pq.push(12);
		pq.push(1);
		pq.push(4);
		pq.push(6);
		pq.push(10);
		while (!pq.empty()) {
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;
	}
}